package com.ph.product.api.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.google.gson.Gson;
import com.ph.base.BaseController;
import com.ph.shopping.common.core.customenum.ResultEnum;
import com.ph.shopping.common.util.page.PageBean;
import com.ph.shopping.common.util.result.Result;
import com.ph.shopping.facade.permission.vo.SessionUserVo;
import com.ph.shopping.facade.product.entity.PhProductProperty;
import com.ph.shopping.facade.product.service.IPhProductClassifyService;
import com.ph.shopping.facade.product.service.IProductPropertyService;
import com.ph.shopping.facade.product.vo.PhProductClassifyVo;
import com.ph.shopping.facade.product.vo.PhProductPropertyVo;

/**
 * @项目：phshopping-api-platform
 * @描述：商品属性查询 @作者： hewl
 * @创建时间：2017年3月6日 上午11:15
 * @Copyright by hewl
 * @version 1.0.0
 */
@Controller
@RequestMapping("/api/product/property")
public class ProductPropertyController extends BaseController {

	private static Logger logger = LoggerFactory.getLogger(ProductPropertyController.class);

	// 引用商品属性service
	@Reference(version = "1.0.0")
	private IProductPropertyService iProductPropertyService;

	// 引用商品分类service
	@Reference(version = "1.0.0")
	private IPhProductClassifyService iPhProductClassifyService;

	/**
	 * 添加属性页面跳转方法
	 * 
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 * @Copyright yyg
	 */
	@RequestMapping(value = "/toAddProperty")
	public String toAddProductPropertyPage(HttpServletRequest request, Model model) throws Exception {
		PhProductClassifyVo phProductClassifyVo = new PhProductClassifyVo();
		// 查询分类级别为一级的数据
		phProductClassifyVo.setClassifyLevel(1);
		phProductClassifyVo.setDeleteFlag(1);
		List<PhProductClassifyVo> list = this.iPhProductClassifyService.getProductClassifyVoDetail(phProductClassifyVo);
		model.addAttribute("list", list);
		return "/product/property/addPrpo";
	}

	/***
	 * 属性添加
	 * 
	 * @param productStr
	 * @param imageStr
	 * @param prpoValStr
	 * @return
	 *
	 */
	@RequestMapping(value = "/add")
	@ResponseBody
	public Result addProductProperty(@ModelAttribute PhProductProperty productProperty, HttpServletRequest request) {
		SessionUserVo info = getLoginUser(request);
		logger.info("属性添加开始，传入参数" + productProperty);
		productProperty.setCreateTime(new Date());
		productProperty.setCreateUser(String.valueOf(info.getId()));
		// 后台数据验证开始
		List<String> errorList = productProperty.validateForm();
		if (errorList != null) {
			Gson gson = new Gson();
			logger.info("信息检索表[com.ph.shopping.facade.product.entity.PhProductProperty]实体中出错验证错误：错误信息如下"
					+ gson.toJson(errorList));
			return getResult(ResultEnum.ENTITY_CHECK_EXCEPTION);
		}
		// 添加开始
		try {
			this.iProductPropertyService.addProductPeoperty(productProperty);
		} catch (Exception e) {
			// e.printStackTrace();
			return getResult(ResultEnum.ADD_PRODUCT_PROPERTY_EXCEPTION);
		}
		logger.info("商品属性添加成功");
		return getResult(ResultEnum.SUCCESS);
	}

	/***
	 * 属性修改跳转
	 * 
	 * @param productStr
	 * @param imageStr
	 * @param prpoValStr
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/toUpdate")
	public String updateProductProperty(@RequestParam("id") Long id, Model model) throws Exception {

		System.out.println("------------------id-------------:" + id);
		// TODO 获取当前的用户
		// SUser user = (SUser)WebUtils.getSessionAttribute(request,
		// Constant.SESSION_USER_KEY);
		// 获取当前修改对象
		PhProductPropertyVo property = new PhProductPropertyVo();
		property.setId(id);
		property = this.iProductPropertyService.getPhProductPropertyVo(property);

		// 通过对象属性：classifyId获取级别和当前分类的数据
		PhProductClassifyVo vo1 = new PhProductClassifyVo();
		vo1.setId(property.getClassifyId());
		PhProductClassifyVo level = iPhProductClassifyService.getProductClassifyVo(vo1);

		// 查询分类级别为一级的数据
		PhProductClassifyVo phProductClassifyVo = new PhProductClassifyVo();
		phProductClassifyVo.setClassifyLevel(1);
		phProductClassifyVo.setDeleteFlag(1);
		List<PhProductClassifyVo> list = this.iPhProductClassifyService.getProductClassifyVoDetail(phProductClassifyVo);
		model.addAttribute("list", list);
		if (level.getClassifyLevel() == 1) {
			model.addAttribute("second", "");
			model.addAttribute("three", "");

			model.addAttribute("theSelectID1", property.getClassifyId());
			model.addAttribute("theSelectID2", "");
			model.addAttribute("theSelectID3", "");
		}
		if (level.getClassifyLevel() == 2) {
			PhProductClassifyVo voSecond = new PhProductClassifyVo();
			voSecond.setClassifyLevel(2);
			voSecond.setParentId(level.getParentId());
			voSecond.setDeleteFlag(1);
			List<PhProductClassifyVo> list2 = this.iPhProductClassifyService.getProductClassifyVoDetail(voSecond);
			model.addAttribute("second", list2);
			model.addAttribute("three", "");
			model.addAttribute("theSelectID1", level.getParentId());
			model.addAttribute("theSelectID2", property.getClassifyId());
			model.addAttribute("theSelectID3", "");
		}
		if (level.getClassifyLevel() == 3) {
			// 查询分类级别为三级的数据
			PhProductClassifyVo voThree = new PhProductClassifyVo();
			voThree.setClassifyLevel(3);
			voThree.setParentId(level.getParentId());
			voThree.setDeleteFlag(1);
			List<PhProductClassifyVo> list3 = this.iPhProductClassifyService.getProductClassifyVoDetail(voThree);
			// 获取三级分类的上级数据
			PhProductClassifyVo voEntity = new PhProductClassifyVo();
			voEntity.setId(level.getParentId());
			voEntity.setClassifyLevel(2);
			voEntity.setDeleteFlag(1);
			PhProductClassifyVo voParent = this.iPhProductClassifyService.getProductClassifyVo(voEntity);

			PhProductClassifyVo voParentBrother = new PhProductClassifyVo();
			voParentBrother.setParentId(voParent.getParentId());
			voParentBrother.setClassifyLevel(2);
			voParentBrother.setDeleteFlag(1);

			List<PhProductClassifyVo> list4 = this.iPhProductClassifyService
					.getProductClassifyVoDetail(voParentBrother);
			model.addAttribute("second", list4);
			model.addAttribute("three", list3);

			model.addAttribute("theSelectID1", voParent.getParentId());
			model.addAttribute("theSelectID2", level.getParentId());
			model.addAttribute("theSelectID3", property.getClassifyId());
		}
		model.addAttribute("property", property);
		return "/product/property/updatePrpo";
	}

	/***
	 * 属性修改
	 * 
	 * @param productStr
	 * @param imageStr
	 * @param prpoValStr
	 * @return
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public Result updateProductProperty(@ModelAttribute PhProductProperty productProperty) {
		// TODO 获取当前的用户
		// SUser user = (SUser)WebUtils.getSessionAttribute(request,
		// Constant.SESSION_USER_KEY);
		logger.info("属性修改开始，传入参数" + productProperty);
		productProperty.setUpdateTime(new Date());
		// 后台数据验证开始
		List<String> errorList = productProperty.validateForm();
		if (errorList != null) {
			Gson gson = new Gson();
			logger.info("信息检索表[com.ph.shopping.facade.product.entity.PhProductProperty]实体中出错验证错误：错误信息如下"
					+ gson.toJson(errorList));
			return getResult(ResultEnum.ENTITY_CHECK_EXCEPTION);
		}
		// 修改开始
		try {
			this.iProductPropertyService.updateProductPeoperty(productProperty);
		} catch (Exception e) {
			// e.printStackTrace();
			return getResult(ResultEnum.UPDATE_PRODUCT_PROPERTY_EXCEPTION);
		}
		logger.info("商品属性修改成功");
		return getResult(ResultEnum.SUCCESS);
	}

	/**
	 * 商品属性主从查询(不要使用分页插件查询)
	 * 
	 * @author hewl
	 * @param phProductPropertyVo
	 * @return Result
	 */
	@RequestMapping(value = "/getPhProductPropertyDetail", method = RequestMethod.GET)
	public @ResponseBody Result getPhProductPropertyDetail(PhProductPropertyVo phProductPropertyVo) throws Exception {
		Result result = new Result();
		result.setData(iProductPropertyService.getPhProductPropertyVoDetail(phProductPropertyVo));
		return result;
	}

	/**
	 * 商品属性列表页面跳转
	 * 
	 * @author hewl
	 * @return String
	 * @throws Exception
	 */
	@RequestMapping(value = "/listPage")
	public String listPage(Model model, String classifyId) throws Exception {
		model.addAttribute("classifyId", classifyId);
		return "/product/property/list/productPropertyList";
	}

	/**
	 * 商品属性列表分页查询
	 * 
	 * @author hewl
	 * @param phProductPropertyVo
	 * @return Result
	 */
	@RequestMapping(value = "/getPhProductPropertyVoPageList", method = RequestMethod.GET)
	public @ResponseBody Object getPhProductPropertyVoPageList(PageBean pageBean,
			PhProductPropertyVo phProductPropertyVo) throws Exception {
		return iProductPropertyService.getPhProductPropertyVoPageList(pageBean, phProductPropertyVo);
	}

	/**
	 * 单条商品属性列表查询
	 * 
	 * @author hewl
	 * @param phProductPropertyVo
	 * @return Result
	 */
	@RequestMapping(value = "/getPhProductProperty", method = RequestMethod.GET)
	public @ResponseBody Result getPhProductProperty(PhProductPropertyVo phProductPropertyVo) throws Exception {
		Result result = new Result();
		result.setData(iProductPropertyService.getPhProductPropertyVo(phProductPropertyVo));
		return result;
	}

	/**
	 * 逻辑删除商品属性
	 * 
	 * @author hewl
	 * @param phProductPropertyVo:id
	 * @return Result
	 */
	@RequestMapping(value = "/deletePhProductProperty", method = RequestMethod.GET)
	public @ResponseBody Result deletePhProductProperty(PhProductPropertyVo phProductPropertyVo) throws Exception {
		return iProductPropertyService.deletePhProductProperty(phProductPropertyVo);
	}
}
